#!/usr/bin/env bash -e

# init
echo "ci ssh script beginning..."

#ssh connect prepare
mkdir -p /root/.ssh
chmod 0700 /root/.ssh

if [ "$DEPLOY_ENV" == "dev" ]; then
  echo "populate dev private key"
  echo "$DEV_SSH_PRIVATE_KEY" > /id_rsa
elif [ "$DEPLOY_ENV" == "staging" ]; then
  echo "populate staing private key"
  echo "$STAGING_SSH_PRIVATE_KEY" > /id_rsa
fi


chmod 0600 /id_rsa
echo -e "Host *\n\tStrictHostKeyChecking no\n\tRequestTTY Yes\n\n" > /root/.ssh/config

# replace war file and restart server
scp -i /id_rsa "$TAR_NAME" ec2-user@"$SERVER_IP":/tmp/
ssh -i /id_rsa ec2-user@"$SERVER_IP" "$DEPLOY_COMMAND"
